package main

import (
	"context"
	"hxzkx/global/model"
	"hxzkx/global/pkg"
	route_budget "hxzkx/kitex_gen/hertz/route_budget"
	"time"
)

// RouteBudgetServiceImpl implements the last service interface defined in the IDL.
type RouteBudgetServiceImpl struct{}

// GetWay implements the RouteBudgetServiceImpl interface.
func (s *RouteBudgetServiceImpl) GetWay(ctx context.Context, req *route_budget.RouteBudgetBuildReq) (resp *route_budget.RouteBudgetBuildResp, err error) {
	// TODO: Your code here...
	var rand = model.RouteInfo{
		FormLetLng: req.FormLocation,
		SeedLetLng: req.SeedLocation,
		UserId:     int(req.UserId),
	}
	model.DB.Create(&rand)
	way, err := pkg.GetWay(req.FormLocation, req.SeedLocation)
	if err != nil {
		return nil, err
	}
	var Ways []*route_budget.TheWay
	var sum int64
	for _, route := range way.Result.Routes[0].Steps {
		var w = route_budget.TheWay{
			LegIndex:  int64(route.LegIndex),
			Direction: int64(route.Direction),
			Distance:  int64(route.Distance),
			RoadName:  route.RoadName,
		}
		Ways = append(Ways, &w)
		sum += int64(route.Distance)
	}
	resp = &route_budget.RouteBudgetBuildResp{
		Desc:     way.Result.Restriction,
		Distance: sum,
		Ways:     Ways,
	}
	return resp, nil
}

// GetPrice implements the RouteBudgetServiceImpl interface.
func (s *RouteBudgetServiceImpl) GetPrice(ctx context.Context, req *route_budget.PriceByDistanceReq) (resp *route_budget.PriceByDistanceResp, err error) {
	// TODO: Your code here...
	price := model.Conf.BaiDuMap.FirstPrice
	if req.Distance-3 > 0 {
		price += float64(req.Distance) * model.Conf.BaiDuMap.KmPrice
	}
	price += float64(req.TimeUse) * model.Conf.BaiDuMap.TimeUse
	news := time.Now()
	if (news.Hour() >= 7 && news.Hour() <= 9) || (news.Hour() >= 17 && news.Hour() <= 19) {
		price *= model.Conf.BaiDuMap.Hot
	}
	resp = &route_budget.PriceByDistanceResp{
		Price: float32(price),
	}
	return resp, nil
}

// GetTime implements the RouteBudgetServiceImpl interface.
func (s *RouteBudgetServiceImpl) GetTime(ctx context.Context, req *route_budget.TimeByDistanceReq) (resp *route_budget.TimeByDistanceResp, err error) {
	// TODO: Your code here...
	return
}
